Symmetric Tree

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree [1,2,2,3,4,4,3] is symmetric:
1
/ \
2 2
/ \ / \
3 4 4 3

题目大意:判断给定二叉树是否是对称的

题目难度:Easy

/**
 * Created by gzdaijie on 16/6/7
 */
public class Solution {
    public boolean isSymmetric(TreeNode root) {
        return helper(root, root);
    }
    private boolean helper(TreeNode p, TreeNode q) {
        if (p == null && q == null) return true;
        if (p == null || q == null) return false;
        return p.val == q.val && helper(p.left, q.right) && helper(p.right, q.left);
    }
}
gzdaijie            updated 2016-06-07 13:27:29

results matching ""

    No results matching ""